using Abp.Domain.Uow;
using Abp.Runtime.Session;
using Castle.Core.Logging;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abp.UI;
using Castle.DynamicProxy;

namespace FwProject.DataFilterFactoryinfo
{
    public class FilterFactoryinfoInterceptor : IInterceptor
    {
        private readonly IAbpSession _abpSession;

        private readonly IUnitOfWork _unitOfWork;

        private readonly IUnitOfWorkManager _unitOfWorkManager;

        private readonly IHttpContextAccessor httpContextAccessor;
        public ILogger Logger { get; set; }
        public FilterFactoryinfoInterceptor(
              IUnitOfWork unitOfWork
            , IAbpSession abpSession
            , IUnitOfWorkManager unitOfWorkManager,
            IHttpContextAccessor _httpContextAccessor)
        {
            Logger = NullLogger.Instance;
            _unitOfWork = unitOfWork;
            _abpSession = abpSession;
            _unitOfWorkManager = unitOfWorkManager;
            httpContextAccessor = _httpContextAccessor;
        }
        public void Intercept(IInvocation invocation)
        {   
            string factoryinfoId=string.Empty;
            try {
            if(httpContextAccessor!=null){
                        var c = httpContextAccessor.HttpContext.User.Claims;
                        factoryinfoId= c?.FirstOrDefault(s => s.Type == FwConsts.FactoryinfoId)?.Value;
                        }
            }catch(Exception e)
            {
                
            }
           
            _unitOfWorkManager.Current.SetFilterParameter(FwConsts.FactroyInfoFilter, FwConsts.FactoryinfoId, factoryinfoId);
           
            //Executing the actual method
            invocation.Proceed();
        }
    }
}
